﻿using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

namespace aztall.Module.BusinessObjects
{
	[DefaultClassOptions, XafDefaultProperty("DisplayText"), ImageName("BO_Product"), NavigationItem("Stock and Pricing")]
	[RuleCombinationOfPropertiesIsUnique("DomainSkuCode", DefaultContexts.Save, "Domain.Oid, SkuCode", CustomMessageTemplate = "Sku Code is existed.", CriteriaEvaluationBehavior = CriteriaEvaluationBehavior.BeforeTransaction | CriteriaEvaluationBehavior.InTransaction)]
	public class Sku : DomainObject
	{
		protected string _SkuCode;
		protected string _SkuName;
		protected double _StdPrice;
		protected double _RetailPrice;
		protected string _CustomerUOM;
		protected string _RetailUOM;
		protected double _UOMRatio;
		protected Status _Status;

		public Sku(Session session)
				: base(session)
		{
		}
		public override void AfterConstruction()
		{
			base.AfterConstruction();
			Status = base.Session.FindObject<Status>(CriteriaOperator.Parse(string.Format("Category = '{0}' && StatusCode = '{1}'", Status.Sku, "Active")));
		}
		protected override void OnSaving()
		{
			base.OnSaving();
		}

		#region own properties
		[RuleRequiredField(DefaultContexts.Save, SkipNullOrEmptyValues = false)]
		[Size(20)]
		[ImportColumn(1), DevExpress.Xpo.DisplayName("Sku Code")]
		public string SkuCode
		{
			get { return _SkuCode; }
			set { SetPropertyValue(nameof(SkuCode), ref _SkuCode, value); }
		}
		[ImportColumn(2)]
		public string SkuName
		{
			get { return _SkuName; }
			set { SetPropertyValue(nameof(SkuName), ref _SkuName, value); }
		}
		[XafDisplayName("Standard Price"), ToolTip("Per Customer UOM"), ImportColumn(4)]
		public double StdPrice
		{
			get { return _StdPrice; }
			set { SetPropertyValue(nameof(StdPrice), ref _StdPrice, value); }
		}
		[XafDisplayName("Retail Std Price"), ToolTip("Per Retail UOM"), ImportColumn(6)]
		public double RetailPrice
		{
			get { return _RetailPrice; }
			set { SetPropertyValue(nameof(RetailPrice), ref _RetailPrice, value); }
		}
		[ImportColumn(3), XafDisplayName("Customer UOM")]
		public string CustomerUOM
		{
			get { return _CustomerUOM; }
			set { SetPropertyValue(nameof(CustomerUOM), ref _CustomerUOM, value); }
		}
		[ImportColumn(5), XafDisplayName("Retail UOM")]
		public string RetailUOM
		{
			get { return _RetailUOM; }
			set { SetPropertyValue(nameof(RetailUOM), ref _RetailUOM, value); }
		}
		[XafDisplayName("UOM Ratio"), ToolTip("UOM Ratio = Qty of Retail UOM Per One Customer UOM.")]
		[RuleRange("UOM Ration > 0", DefaultContexts.Save, 0, int.MaxValue, ResultType = ValidationResultType.Error, CustomMessageTemplate = "UOM ration should be greater than zero.")]
		[ImportColumn(7)]
		public double UOMRatio
		{
			get { return _UOMRatio; }
			set { SetPropertyValue(nameof(UOMRatio), ref _UOMRatio, value); }
		}
		[DataSourceCriteria("Category ='" + Status.Sku + "'")]
		[RuleRequiredField(DefaultContexts.Save, SkipNullOrEmptyValues = false)]
		public Status Status
		{
			get { return _Status; }
			set { SetPropertyValue(nameof(Status), ref _Status, value); }
		}
		#endregion
		[VisibleInListView(false), VisibleInDetailView(false), XafDisplayName("Sku List")]
		public string DisplayText
		{
			get { return string.Format("{0}: {1}", SkuCode, SkuName); }
		}
		#region collections
		[Association("Promo_Sku", UseAssociationNameAsIntermediateTableName = true), DevExpress.Xpo.DisplayName("Promotions")]
		[DataSourceCriteria("Domain.Oid = '@This.Domain.Oid'"), VisibleInDetailView(false)]
		public XPCollection<Promo> Promos
		{
			get { return GetCollection<Promo>(nameof(Promos)); }
		}
		#endregion
	}
}
